package cn.keniykey.mc.landterritory.command.landadmin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import cn.keniykey.mc.landterritory.command.AbCommandBase;
import cn.keniykey.mc.landterritory.command.CommandUtil;
import cn.keniykey.mc.landterritory.config.LanguageManager;

public final class CommandLandAdminMain implements TabExecutor{
	private final List<AbCommandBase> commands;
	
	public CommandLandAdminMain() {
		this.commands = this.loadCommands();
	}
	
	private List<AbCommandBase> loadCommands() {
		final List<AbCommandBase> list = new ArrayList<>(4);
		list.add(new CommandReload());
		list.add(new CommandAbout());
		list.add(new CommandFlag());
		list.add(new CommandLand());
		return list;
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (args.length > 0) {
			final String comstr = args[0].toLowerCase();
			if (comstr.equals("help")) {
				sender.sendMessage(LanguageManager.getLang(1203000));
				for(final AbCommandBase base : this.commands) {
					if (base.getPermission() == null || sender.hasPermission(base.getPermission())) sender.sendMessage(base.usage());
				}
				return true;
			}
			for(final AbCommandBase base : this.commands) {
				if (base.getInstruction().equals(comstr)) {
					if (base.isMustPlayer() && !(sender instanceof Player)) {
						CommandUtil.sendMustPlayer(sender);
						return true;
					}
					if (base.getPermission() != null && !sender.hasPermission(base.getPermission())) {
						CommandUtil.sendNotPermission(sender); //提示玩家无权限
						return true;
					}
					final List<String> parameter = new ArrayList<>(Arrays.asList(args));
					parameter.remove(0);
					try {
						if (!base.execute(sender, parameter)) CommandUtil.sendCommandUsage(sender, base.usage());
					}catch(Exception ex) {
						ex.printStackTrace();
					}
					return true;
				}
			}
		}
		CommandUtil.sendCommandUseHelp(sender, label); //提示玩家输入help
		return true;
	}
	@Override
	public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
		List<String> list = null;
		if (args.length > 1) {
			final String comstr = args[0].toLowerCase();
			for(final AbCommandBase base : this.commands) {
				if (base.getInstruction().equals(comstr)) {
					if (base.getPermission() == null || sender.hasPermission(base.getPermission())) {
						final List<String> parameter = new ArrayList<>(Arrays.asList(args));
						parameter.remove(0);
						try {
							list = base.prompt(sender, parameter);
						}catch(Exception ex) {
							ex.printStackTrace();
						}
						break;
					}
				}
			}
		}else {
			list = new ArrayList<>(this.commands.size()+1);
			list.add("help");
			for(final AbCommandBase base : this.commands) {
				if (base.getPermission() == null || sender.hasPermission(base.getPermission())) list.add(base.getInstruction());
			}
		}
		if (list != null && !list.isEmpty()){
			final String str = args[args.length - 1];
			final Iterator<String> it = list.iterator();
			while(it.hasNext()) {
				if (!it.next().contains(str)) it.remove();
			}
		}
		return list;
	}
}